//
//  CMDBEditProductionCollectionView.swift
//  CMDB
//
//  Created by 曹桂祥 on 2017/5/18.
//  Copyright © 2017年 CMDB. All rights reserved.
//

import UIKit

private let EditCellID = "EditCell"

private let reuseHeader: String = "ShowTitleView"

class CMDBEditProductionCollectionView : BaseCollectionView {
    
    var itemWidth : CGFloat?
    
    var allTags : [CMDBShowTags] = [CMDBShowTags]()
    
    var sectionTitles : [String] = [String]()
    
    var longPress : UILongPressGestureRecognizer!
    
//    override class func getInstance(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) -> Self {
//        
//        if let layout = layout as! BaseFlowLayout
//        
//        setupLayout(layout)
//        
//        return self.init(frame: frame, collectionViewLayout: layout)
//    }
    
    override func commitInitOwnProperties() {
        
        super.commitInitOwnProperties()
        
        backgroundColor = UIColor.whiteColor()
        
        registerClass(CMDBTagCollectionCell.self, forCellWithReuseIdentifier: EditCellID)
        
        registerClass(CMDBEditTagHeaderView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: reuseHeader)
        
        longPress = UILongPressGestureRecognizer(target: self, action: #selector(lonePressMoving(_:)))
        
        addGestureRecognizer(longPress)
    }
}


extension CMDBEditProductionCollectionView{
    
   override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    
        return allTags.count
    }
    
   override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return allTags[section].tags.count
    }
    
   override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(EditCellID, forIndexPath: indexPath) as! CMDBTagCollectionCell
        
        cell.backgroundColor = UIColor.randomColor()
        
        cell.tagModel = allTags[indexPath.section].tags[indexPath.item]
        
        return cell
        
    }
    
    func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
        
        if kind == UICollectionElementKindSectionHeader {
            
            let header = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: reuseHeader, forIndexPath: indexPath) as! CMDBEditTagHeaderView
            
            header.textLabel.text = sectionTitles[indexPath.section]
            
            return header
        } else {
            
            return UICollectionReusableView()
        }
    }
    
    override func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        
        return CGSizeMake(CGRectGetWidth(bounds), 20)
    }
}

//MARK:- Gester actions
extension CMDBEditProductionCollectionView{
    @objc func lonePressMoving(gester : UILongPressGestureRecognizer){
        
        switch gester.state {
        case .Began:
            
            let selectedIndexPath = indexPathForItemAtPoint(gester.locationInView(self))
            
            beginInteractiveMovementForItemAtIndexPath(selectedIndexPath!)
            
            break
        case .Changed:
            updateInteractiveMovementTargetPosition(gester.locationInView(self))
            
            break
            
        case .Ended:
            endInteractiveMovement()
            
            break
        default:
            cancelInteractiveMovement()
            
            printLog("default")
        }
        
        
    }
}
